<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';


//delete
if($_GET[action]=='del'){
	if(!empty($_POST[del])){
	  foreach($_POST[del] as $value){
	     $str[] = "`jo_id` = '".$value."' ";
	  }
	  $where = @implode(' OR ',$str);
	  $sql = "DELETE FROM pa_job WHERE ($where) AND jo_utype = '".$_SESSION[usertype]."' AND jo_uid = '".$_SESSION[uid]."' ";
	  $DB->query($sql);
	}
	   echo "<meta http-equiv=\"refresh\" content=\"0;URL=mypostjobs.php\">";
	   exit;
}

require_once './include/advisory_db.php';
//print_r($_SESSION);
$smarty->compile_check = true;

$smarty->display('header.tpl');
if($_SESSION[usertype]=="employer"){
$sql = "SELECT ep_copname FROM pa_employer WHERE ep_id='".$_SESSION[uid]."'";
//echo $sql;
$ar = $DB->fetch_one_array($sql);
//print_r($arr);
$smarty->assign("copname","-".$ar[ep_copname]);
}

//getvalues
if($_GET[country]==''){
$_GET[country] = 211;
}
$smarty->assign('s',$_GET);
//jobupdate
$smarty->assign('jobupdate_names',$UPDATE);
$smarty->assign('state',$state);
$smarty->assign('country',$country);
$smarty->assign('industry',$jobcategory);
$smarty->assign('function',$jobfunction);
$smarty->assign('status',$jobfillstatus);
$smarty->assign('jobtype',$jobtype);
$smarty->assign('rec_myjob',$rec_myjob);


//print_r($findjob_orderby3);

$where = " WHERE `jo_utype`='".$_SESSION[usertype]."' AND `jo_uid`='".$_SESSION[uid]."' ";
$order = "  GROUP BY jo_id ORDER BY a.jo_id DESC ";

if($_GET[action]=="search"){ 
	//print_r($_GET);
	if($_SESSION[usertype]=="employer"){
			if($_GET[copname]!=''){ $arr[]  = " `jo_copname`  like '%".$_GET[copname]."%'  ";}
			if($_GET[username]!=''){ $arr[] = " `jo_username` like '%".$_GET[username]."%' ";}
			if($_GET[title]!=''){ $arr[]    = " `jo_title` like '%".$_GET[title]."%'  "; }
			if($_GET[orderby]=='0'){$by = "jo_id";}
			else{
				$ord   = array('','jo_title','jo_city','jo_state','jo_country','jo_function','jo_status','jo_type','jo_copreq','jo_regtime');
				$by    = $ord[$_GET[orderby]];
			}
	}else{	
		    if($_GET[keywords]!=''){ $arr[] = " (`jo_title`  like '%".$_GET[keywords]."%'  OR   `jo_city`  like '%".$_GET[keywords]."%' OR  `jo_description`  like '%".$_GET[keywords]."%') ";}	
			if($_GET[title]!=''){ $arr[]    = " `jo_title` like '%".$_GET[title]."%'  "; }
			if($_GET[orderby]=='0'){$by = "jo_id";}
			else{
				$ord   = array('','jo_title','jo_city','jo_state','jo_country','jo_function','jo_status','jo_type','jo_copreq','jo_regtime');
				$by = $ord[$_GET[orderby]];
			}
	}
	if(empty($by)){ $by = "jo_id";}
	if($_GET[city]!='') { $arr[] = " `jo_city`  like '%".$_GET[city]."%'  "; }
	if($_GET[state]!='0'&&$_GET[state]!='') { $arr[] = " `jo_state` = '".$_GET[state]."'  ";}
	if($_GET[country]!='0'&&$_GET[country]!='') { $arr[] = " `jo_country` = '".$_GET[country]."'  "; }
	if($_GET[status]!='0'&&$_GET[status]!='') { $arr[] = " `jo_status` = '".$_GET[status]."'  ";}
	if($_GET['function']!='0'&&$_GET['function']!=''){ $arr[] = " `jo_function` = '".$_GET['function']."' ";}
	if($_GET[jobtype]!='0'&&$_GET[jobtype]!='') { $arr[] = " `jo_type`  = '".$_GET[jobtype]."'  ";}
	//if($_GET[postdate]!='')  { $arr[] = " `jo_regtime`  like '%".$_GET[postdate]."%'  ";}
	if($_GET[copreq]!='0'&&$_GET[copreq]!=''){ $arr[] = " `jo_copreq` like '%".$_GET[copreq]."%'  ";}
	if($_GET[jobupdate]!='') { $arr[] = " (UNIX_TIMESTAMP( ) - `jo_updtime`) < (3600*24*".$_GET[jobupdate].")" ;}



//print_r($arr);
if(!empty($arr)){
	$implode  = @implode("AND", $arr);
	$implode  = " AND ".$implode;
	$where    .= $implode;
}


    $order = " GROUP BY jo_id  ORDER BY $by DESC";
}

if($_GET[tp]==1){
	
	$by    = "a.jo_id"; 
	$order = "DESC" ;

	$_GET[order1]==''&&$order1=1;$_GET[order2]==''&&$order2=1;$_GET[order3]==''&&$order3=1;$_GET[order4]==''&&$order4=1;
	$_GET[order5]==''&&$order5=1;$_GET[order6]==''&&$order6=1;$_GET[order7]==''&&$order7=1;
	switch($_GET[swh]){
	   case 1:
		   $by = "a.jo_id";
		   if($_GET[order1]==1){$order_arr[1]=2; $order=" DESC ";$image_arr[1] = "<img src=\"images/dot1.gif\" align=\"absmiddle\">";}else{$order_arr[1]=1;$order=" ASC ";$image_arr[1] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 2:
		   $by = "a.jo_contact";
		   if($_GET[order2]==1){$order_arr[2]=2; $order=" DESC ";$image_arr[2] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[2]=1;$order=" ASC ";$image_arr[2] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 3:
			$by = "a.jo_regtime";
		   if($_GET[order3]==1){$order_arr[3]=2; $order=" DESC ";$image_arr[3] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[3]=1;$order=" ASC ";$image_arr[3] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 4:
		   $by = "a.jo_status";
		   if($_GET[order4]==1){$order_arr[4]=2; $order=" DESC ";$image_arr[4] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[4]=1;$order=" ASC ";$image_arr[4] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
		case 5:
		   $by = "a.jo_title";
		   if($_GET[order5]==1){$order_arr[5]=2; $order=" DESC ";$image_arr[5] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[5]=1;$order=" ASC ";$image_arr[5] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 6:
		   $by = "a.jo_status";
		   if($_GET[order6]==1){$order_arr[6]=2; $order=" DESC ";$image_arr[6] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[6]=1;$order=" ASC ";$image_arr[6] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 7:
		   $by = " count ";
		   if($_GET[order7]==1){$order_arr[7]=2; $order=" DESC ";$image_arr[7] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[7]=1;$order=",app_ownid ASC ";$image_arr[7] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	}
		$smarty->assign("order_arr",$order_arr);
		$smarty->assign("image_arr",$image_arr);
     $order = " GROUP BY jo_id  ORDER BY $by $order";

}
//echo "SELECT COUNT(*) AS count FROM pa_job as a ,pa_applicant as b $where $order";

$nav = new buildNav;
$nav->limit=25;

$result = $DB->query("SELECT COUNT(*) AS count FROM  pa_applicant as b RIGHT JOIN pa_job as a ON a.jo_id = b.app_jobid $where $order");
$count = 0;
while($list = $DB->fetch_array($result)){
  $count++;
}

//$total = $DB->fetch_one_array("SELECT COUNT(*) AS count FROM pa_job as a ,pa_applicant as b $where $order");
$nav->total_result =  $count;
//echo "SELECT COUNT(*) AS count,a.*,b.* FROM pa_job as a ,pa_applicant as b $where $order";
$nav->execute("SELECT COUNT(*) AS count,a.*,b.*  FROM  pa_applicant as b RIGHT JOIN pa_job as a ON a.jo_id = b.app_jobid $where $order");

$page = $nav->pagenav();
//echo $sql2;

$i=0;
while($list2 = $DB->fetch_array($nav->sql_result)){
   $datajob[$i]              = $list2;
   $datajob[$i][jo_state]    = $state[$list2[jo_state]];
   $datajob[$i][jo_country]  = $country[$list2[jo_state]];
   $datajob[$i][jo_function] = $jobfunction[$list2[jo_function]];
   if(preg_match("/2|3/",$list2[jo_contact])){
		$datajob[$i][jo_posttonetwork] = '<font color="orange">Posted</font>';
   }elseif($list2[jo_status] == 3){
		$datajob[$i][jo_posttonetwork] = 'Closed';
   }else{
		$datajob[$i][jo_posttonetwork] = 'Private';
   }
   $datajob[$i][jo_status]   = $jobfillstatus[$list2[jo_status]];
   $datajob[$i][jo_priority] = $priority[$list2[jo_priority]];
   $datajob[$i][jo_type]     = $jobtype[$list2[jo_type]];
   $datajob[$i][jo_regtime]  = date("m/d/Y",$list2[jo_regtime]);
   $datajob[$i][jo_applicants] = getNumberOfApplicants($datajob[$i][jo_id]);
   $datajob[$i][jo_contacts] = getNumberOfContacted($datajob[$i][jo_id]);
   $datajob[$i][jo_approved] = getNumberOfApproved($datajob[$i][jo_id]);
   if($datajob[$i][jo_applicants] < 1 && $datajob[$i][jo_contacts] < 1){
      $datajob[$i][candel] = 1;
   }
   $i++;
}
$smarty->assign("joblist",$datajob);
$smarty->assign("page",$page);
if($err!=''){
$smarty->assign("err",$err);
}
$smarty->assign("search",$reg_data);
$smarty->display('allleft.tpl');
$smarty->display('mypostjobs.tpl');
$smarty->display('footer.tpl');
?>